<template>
  <view class="container">
    <FilterNavbar :searchList="searchList" v-model:queryParam="queryParam" @confirm="onFilterNavbarConfirm">
      <template #center>
        <view class="navbar-title"> 处置车辆 </view>
      </template>
    </FilterNavbar>
    <view class="manager-search">
      <ve-search-bar :showBorder="true" placeholder="请输入车牌号/车型名称" @search="searchBarSearch" />
    </view>
    <scroll-view
      scroll-y
      :scroll-top="scrollTop"
      :scroll-with-animation="true"
      refresher-enabled
      :refresher-triggered="refreshStatus"
      :enable-back-to-top="true"
      @refresherrefresh="resetRefresh(true)"
      @scrolltolower="reachBottom"
    >
      <view class="record-list">
        <block v-for="item in dataSource" :key="item">
          <ManagerItem :manager-item="item" :resetRefresh="resetRefresh" from="car-disposal" />
        </block>
      </view>
      <!-- 加载结束列表为空时展示 -->
      <ve-empty v-if="dataSource.length == 0 && loadStatus == 'nomore'"></ve-empty>
      <!-- 底部加载状态 -->
      <ve-load-more v-else :status="loadStatus" bgColor="#F7F7F9"></ve-load-more>
    </scroll-view>
  </view>
</template>

<script setup lang="ts">
  import FilterNavbar from '@/components/FilterNavbar'
  import ManagerItem from './components/manager-item.vue'

  import { computed, ref } from 'vue'
  import { usePageDict } from '@/hooks/useDict'
  import useList from '@/hooks/useList'
  import { onLoad, onUnload } from '@dcloudio/uni-app'

  const dict = usePageDict('carManagerToListDict')

  const queryParam = ref<any>({
    siteId: undefined,
    siteName: undefined,
    pageNumber: 1,
  })

  const { resetRefresh, reachBottom, loadData, scrollTop, loadStatus, refreshStatus, dataSource } = useList(
    '/V5/carInfo/disposalList',
    queryParam,
  )

  const searchList = computed(() => {
    return [
      {
        prop: 'siteId',
        label: '所属门店',
        type: 'remote-select',
        attrs: {
          label: queryParam.value.siteName,
          type: 'site',
        },
      },
      {
        prop: 'source',
        label: '车辆来源',
        type: 'picker',
        attrs: {
          options: dict.disposalSourceEnumList,
        },
      },
    ]
  })

  const searchBarSearch = (val: any) => {
    queryParam.value = {
      ...queryParam.value,
      keyword: val,
    }
    resetRefresh()
  }

  const onFilterNavbarConfirm = (params: any) => {
    const tagStatusList = Array.isArray(params.tagStatusList) ? params.tagStatusList : [params.tagStatusList]
    queryParam.value = {
      ...queryParam.value,
      ...params,
      tagStatusList: params.tagStatusList ? tagStatusList : undefined,
    }
    resetRefresh()
  }

  uni.$on('getList', function () {
    queryParam.value.pageNumber = 1
    dataSource.value = []
    loadData()
  })

  onLoad((options = {}) => {
    if (options.siteId) {
      queryParam.value.siteId = options.siteId
      queryParam.value.siteName = options.siteName
    }
    loadData()
  })

  onUnload(() => {
    uni.$off('repliceVisibleFn')
    uni.$off('disposeVisibleFn')
    uni.$off('getList')
  })
</script>

<style lang="less" scoped>
  .container {
    height: 100vh;
    background-color: rgba(247, 248, 250, 1);
  }
  .record-list {
    padding: 24rpx 24rpx;
  }
  .manager-search {
    position: sticky;
    top: 44px;
    // z-index: 999;
  }
  .manager-tabs {
    background-color: #fff;
    margin-top: -20rpx;
  }
  .ve-bottom-box {
    height: 132rpx;
  }
</style>
